<?php

namespace App\Models\Stock;

use App\Models\Steel\SteelMaterialPriceCustom;
use App\Models\Steel\SteelMaterialPriceSupply;
use App\Models\Warehouse\WarehouseList;
use App\Services\Common\Common;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Stock extends Model
{
    protected $table = 'stock';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $cpid, $utype = 0)
    {
        $query = self::with(['customprice', 'suppleyprice', 'warehousename', 'substock'])->where(['cpid' => $cpid]);

        $archive_no = $request->archive_no;
        if ($archive_no != '') {
            $query->where('archive_no', 'like', '%' . $archive_no . '%');
        }
        $archive_name = $request->archive_name;
        if ($archive_name != '') {
            $query->where('archive_name', 'like', '%' . $archive_name . '%');
        }

        $archive_name_all=$request->archive_name_all;

        if ($archive_name_all != '') {
            $query->where('archive_name',$archive_name_all);
        }

        $archive_spec = $request->archive_spec;
        if ($archive_spec != '') {
            $query->where('archive_spec', 'like', '%' . $archive_spec . '%');
        }
        $archive_material = $request->archive_material;
        if ($archive_material != '') {
            $query->where('archive_material', 'like', '%' . $archive_material . '%');
        }

        $steel_factory_name = $request->steel_factory_name;
        if ($steel_factory_name != '') {
            $query->where('steel_factory_name', 'like', '%' . $steel_factory_name . '%');
        }
        $warehouse_id = intval($request->warehouse_id);
        if ($warehouse_id > 0) {
            $query->where('warehouse_id', $warehouse_id);
        }

        $is_have_virtual = intval($request->is_have_virtual) ?? 0;
        if ($is_have_virtual>0) {
            $is_have_virtual==1 && $query->where('archive_type', 4);
            $is_have_virtual==2 && $query->where('archive_type','<>', 4);
        }

        //$mangerinfo=Common::MangerInfo($request);

//        if(isset($utype) && $utype==7){
//            $query->where(function($q) use ($mangerinfo) {
//                 $q->where('warehouse_type','<>',3)->orWhere([['warehouse_type',3],['manger_id',$mangerinfo['id']]]);
//            });
//        }


        $withoutids=is_array($request->withoutids)?$request->withoutids:[];

        if(count($withoutids)){
            $query->whereNotIn('id',$withoutids);
        }



        $is_have_count = $request->is_have_count;
        if ($is_have_count == 1) { //库存大于0
            $query->where('can_use_pieces', '>', 0);
        }
        if ($is_have_count == 2) { //库存小于登录0
            $query->where('can_use_pieces', '<=', 0);
        }

        $status = $request->status;

        if($status>0){
            $query->where('status', $status);
        }


        $isgetall=$request->isgetall ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->orderBy('create_at', 'desc')->paginate($limit)->toArray();

            return paginateDataToArray($list);
        }else{
            $list = $query->orderBy('create_at', 'desc')->get()->toArray();

            return ['list' => $list];
        }
    }

    public function customprice()
    {
        // return $this->hasOne(SteelMaterialPriceCustom::class,'material_attr_id','material_id')->select(['base_price','add_price','price']);
        return $this->hasOne(SteelMaterialPriceCustom::class, 'material_attr_id', 'material_id');
    }

    public function suppleyprice()
    {
        return $this->hasOne(SteelMaterialPriceSupply::class, 'material_attr_id', 'material_id');
    }

    public function warehousename()
    {
        return $this->hasOne(WarehouseList::class, 'id', 'warehouse_id');
    }

    public function substock()
    {
        return $this->hasMany(StockSub::class, 'stock_id', 'id');
    }

    public function addAll(array $data)
    {
        return DB::table($this->getTable())->insert($data);
    }


    public function getCanUseWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getLockedWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getCurrentWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getStorageWaitingWeightAttribute($value)
    {
        return $value / 10;
    }
    public function getAvgWightAttribute($value)
    {
        return $value / 10;
    }
}
